package com.devin.controller;
/**
 *  
 *  @author devin
 *  @date 2020-04-17 13:51
 *  @version 1.0.2
*/
import com.devin.dto.UploadFileResponse;
import com.devin.mail.MailTool;
import com.devin.service.FileService;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.testng.annotations.Test;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Slf4j
//@NoArgsConstructor
@RestController
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    private final FileService fileService;

    @Autowired
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @PostMapping("/uploadFile")
    public HashMap<String,Object> uploadFile(@RequestParam("file") MultipartFile file,@RequestParam String mail) throws Exception {


        //上传文件的
        HashMap<String,Object> objectHashMap = new HashMap<>(1);
//        String pattern = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
//        boolean matches = Pattern.matches(pattern, mail);
        if(!checkMail(mail)){
            objectHashMap.put("message","你的邮箱有误");
            objectHashMap.put("code",0);
            return objectHashMap;
        }
        //如果邮箱效验没问题   发送邮件

        //存储文件
        Boolean status = fileService.storeFile(file);
        if(status){
            MailTool.sendMail(mail,file.getOriginalFilename());
            objectHashMap.put("message","上传后台成功");
            objectHashMap.put("code",1);
        }
        else{
            objectHashMap.put("message","上传电影失败");
            objectHashMap.put("code",0);
        }
        return objectHashMap;
    }

//    @Test
    public Boolean checkMail(String mail){
//        System.out.println("ss");
//        String mail = "225604471@qq.com";
        String pattern = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
        Pattern compile = Pattern.compile(pattern);
        Matcher matcher = compile.matcher(mail);
        return matcher.find();


    }

//    @Test
    public void test5(){
                String mail = "225604471@qq.com";
        System.out.println(checkMail(mail));
    }
//    @PostMapping("/uploadMultipleFiles")
//    public List<UploadFileResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
//        return Arrays.stream(files)
//                .map(this::uploadFile)
//                .collect(Collectors.toList());
//    }


}